//
//  BottomTypeView.m
//  PingTu
//
//  Created by Yangfan on 15/2/5.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "BottomTypeView.h"
#import "FilesOperation.h"

@implementation BottomTypeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self DataLoad];
    }
    return self;
}

- (void)DataLoad {
}

- (void)UI {
    _bottomTypeScroll = [[UIScrollView alloc] init];
    [_bottomTypeScroll setShowsHorizontalScrollIndicator:NO];
    [_bottomTypeScroll setFrame:CGRectMake(0, 0, viewWidth, viewHeight)];
    [_bottomTypeScroll setBackgroundColor:[UIColor clearColor]];
    [self addSubview:_bottomTypeScroll];
    
    // 获取当前有多少张图片模板在用户当前选择的图片数目中
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[FilesOperation returnResourceFileAddress:@"HowManyMuBanInOneType" andTyeo:@"plist"]];

    // 每一个模板的展示
    UIImageView *showOneMuBan;
    UIButton *upMuBanButton;
    [_bottomTypeScroll setContentSize:CGSizeMake(7 + 63 * [[dic objectForKey:[NSString stringWithFormat:@"%d", _picNumber]] intValue], 94)];
    for (int i = 0; i < [[dic objectForKey:[NSString stringWithFormat:@"%d", _picNumber]] intValue]; i++) {
        showOneMuBan = [[UIImageView alloc] init];
        [showOneMuBan setTag:9999 + i];
        [showOneMuBan setUserInteractionEnabled:YES];
        showOneMuBan.layer.borderColor = [HexRGB(0xf84a2c) CGColor];
        
        if (i == 0) {
            showOneMuBan.layer.borderWidth = 2;
        } else {
            showOneMuBan.layer.borderWidth = 0;
        }
        NSString *str = [NSString stringWithFormat:@"%dpicForStyle%d.jpg", _picNumber, i];
        [showOneMuBan setImage:[UIImage imageNamed:str]];
        [showOneMuBan setBackgroundColor:[UIColor yellowColor]];
        [showOneMuBan setFrame:[self returnCoverRect:showOneMuBan.image andIndex:i]];// 将要显示每一个示例图片的大小
        [_bottomTypeScroll addSubview:showOneMuBan];
        
        upMuBanButton = [[UIButton alloc] initWithFrame:showOneMuBan.bounds];
        [upMuBanButton setTag:8888 + i];
        [upMuBanButton addTarget:self action:@selector(selectOneMuBan:) forControlEvents:UIControlEventTouchUpInside];
        [showOneMuBan addSubview:upMuBanButton];
        if (i == 0) {
            _preButton = upMuBanButton;
        }
    }
}

- (void)firstInto {
    [self UI];
    UIButton *button = [[UIButton alloc] init];
    button.tag = 8888;
    [self selectOneMuBan:button];
}

/**
 *  点击一个图片模板后的事件
 *
 *  @param sender
 */
- (void)selectOneMuBan:(UIButton *)sender {
//    for (UIView *view in [_bottomTypeScroll subviews]) {
//        if (view.tag >= 9999) {
//            view.layer.borderWidth = 0;
//        }
//    }
    [_bottomTypeScroll viewWithTag:_preButton.tag + 1111].layer.borderWidth = 0;
    CGRect rect = [[_bottomTypeScroll viewWithTag:_preButton.tag + 1111] frame];
    [[_bottomTypeScroll viewWithTag:_preButton.tag + 1111] setFrame:CGRectMake(rect.origin.x + 2, rect.origin.y + 2, rect.size.width - 4, rect.size.height - 4)];
    _preButton = sender;
    
    [_bottomTypeScroll viewWithTag:sender.tag + 1111].layer.borderWidth = 2;
    rect = [[_bottomTypeScroll viewWithTag:sender.tag + 1111] frame];
    [[_bottomTypeScroll viewWithTag:sender.tag + 1111] setFrame:CGRectMake(rect.origin.x - 2, rect.origin.y - 2, rect.size.width + 4, rect.size.height + 4)];
    
    if (_delegate && [_delegate respondsToSelector:@selector(selectWhichTypeMuBan:)]) {
        [_delegate selectWhichTypeMuBan:[self returnIntFromNSUInteger:sender.tag - 8888]];
    }
}

/**
 *  将要显示每一个示例图片的大小
 *
 *  @param image 将要显示的封面
 *  @param index 封面所在的索引位置
 *
 *  @return 返回图片的Rect值
 */
- (CGRect)returnCoverRect:(UIImage *)image andIndex:(int)index {
    CGRect rect;
    float imageWidth = image.size.width;
    float imageHeight = image.size.height;
    imageWidth = 56;
    imageHeight = 56.0 * imageHeight / image.size.width;
    rect.origin.x = 7 + 63 * index;
    rect.origin.y = (viewHeight - imageHeight) / 2.0;
    rect.size.width = imageWidth;
    rect.size.height = imageHeight;
    return rect;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
